/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.util;

import com.servengine.portal.PortalManagerLocal;
import com.servengine.user.UserManagerLocal;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ExpensiveDataFactory
{
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog( ExpensiveDataFactory.class.getName() );
	protected HashMap<String,Map<String,ExpensiveDatum>> expensiveData = new HashMap<String,Map<String,ExpensiveDatum>>();
	protected PortalManagerLocal portalmanager;
	protected UserManagerLocal usermanager;

	public ExpensiveDataFactory(UserManagerLocal usermanager, PortalManagerLocal portalmanager)
	{
		this.usermanager=usermanager;
		this.portalmanager=portalmanager;
	}

	protected class ExpensiveDatum
	{
		Object datum;
		long lastRefreshed;
		ExpensiveDatum(Object value)
		{
			datum=value;
			lastRefreshed=new java.util.Date().getTime();
		}
	}
	//TODO Añadir borrado de items por tiempo
	public boolean hasExpensiveDatum(String portalid, String key)
	{
	  Map<String,ExpensiveDatum> portalData = getPortalData(portalid);
	  return portalData.containsKey(key) && portalData.get(key).lastRefreshed+(5*60*1000)>new Date().getTime();
	}
	public Object getExpensiveDatum(String portalid, String key) throws IllegalStateException
	{
		return getExpensiveDatum(portalid, key,5*60*1000);
	}
	public Object getExpensiveDatum(String portalid, String key, long maxLifeTime) throws IllegalStateException
	{
		if (log.isDebugEnabled()) log.debug("Returning "+key+"@"+"portalid");
		Map<String,ExpensiveDatum> portalData = getPortalData(portalid);
		if (portalData.containsKey(key) && portalData.get(key).lastRefreshed+maxLifeTime>new Date().getTime())
			return portalData.get(key).datum;
		ExpensiveDatum value = getFromFactory(portalid,key);
		if (value==null)
			return null;
		portalData.put(key,value);
		return value.datum;
	}
	/**
	 * Para su uso desde el exterior
	 * @param portalid
	 * @param key
	 * @param object
	 */
	public void setExpensiveDatum(String portalid, String key, Object object)
	{
	  getPortalData(portalid).put(key,new ExpensiveDatum(object));
	}
	protected Map<String,ExpensiveDatum> getPortalData(String portalid)
	{
	  Map<String,ExpensiveDatum> portalData = expensiveData.get(portalid);
	  if (portalData==null)
	  {
	    portalData = new HashMap<String,ExpensiveDatum>();
	    expensiveData.put(portalid,portalData);
	  }
		return portalData;
	}
	
	protected ExpensiveDatum getFromFactory(String portalid, String key)
	{
		try
		{
			if (key.equals("userCount"))
			{
				if (portalid==null)
					return new ExpensiveDatum(usermanager.getUserCount());
				else
					return new ExpensiveDatum(portalmanager.getPortal(portalid).getUserCount());
			}
			return null;
		} catch (Exception e)
		{
			throw new IllegalStateException(e.getMessage(),e);
		}
	}
}
